#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "address_book_system.h"

//用户注册功能
int User_Registration_Features()
{
    FILE* fp;
    seqlist_t writeuser;
    seqlist_t readuser;
    seqlist_t* writeadd=&writeuser;
    int count=0;
    int flag=1;

    writeadd=CreateEpSeqlist();

    fp=fopen("userdata.bin","a+");//打开存储文件
    if (NULL==fp)
    {
        perror("user fopen err\n");
        return -1;
    }
    
    printf("***********************************\n");
    printf("您已进入注册系统\n");
    while (1)
    {
        printf("请输入注册的帐号(输入*返回上一级):\n");
        scanf("%s",writeuser.account);
        fseek(fp,0,SEEK_SET);
        flag=1;
        if (writeuser.account[0]=='*' && writeuser.account[1]=='\0')
        {
            return 0;
        }
        while (fread(&readuser,sizeof(seqlist_t),1,fp)!=0)
        {
            if (strcmp(readuser.account,writeuser.account)==0)
            {
                flag=0;
                break;
            }
        }
        if (flag==0)
        {
            printf("帐号重复，请重新输入帐号!\n");
        }
        else
        {
            break;
        }
    }

    while (1)
    {
        printf("请输入密码:\n");
        scanf("%s",writeuser.password);
        char temppassword[32]={'\0'};
        printf("请再次输入密码:\n");
        scanf("%s",temppassword);
        if (strcmp(writeuser.password,temppassword)==0)
        {
            break;
        }
        printf("输入密码不同请重新输入:\n");
    }
    printf("***********************************\n");

    fwrite(&writeuser,sizeof(seqlist_t),1,fp);
    printf("用户注册成功!\n");

    
    free(writeadd);
    fclose(fp);
    return 0;
}


//用户注销功能
void User_Deregistration_Features(int usernum)
{
    FILE* fp;
    int choose=1;
    int i=0;
    seqlist_t readuser;
    char userpassword[32]={'\0'};

    fp=fopen("userdata.bin","r+");//打开存储文件
    if (NULL==fp)
    {
        perror("user fopen err\n");
        return;
    }

    printf("usernum:%d\n",usernum);
    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fread(&readuser,sizeof(seqlist_t),1,fp);

    printf("\n");
    printf("**********************\n");
    printf("您已进入用户注销系统:\n");
    printf("请问您是否需要注销该用户(需要则输入0):\n");
    scanf("%d",&choose);
    if(choose!=0)
    {
        fclose(fp);
        return;
    }
    while (1)
    {
        printf("请输入用户密码(输入*退出注销):\n");
        scanf("%s",userpassword);
        if (userpassword[0]=='*'&&userpassword[1]=='\0')
        {
            fclose(fp);
            return;
        }
        else if(strcmp(userpassword,readuser.password)==0)
        { 
            memset(readuser.account,'\0',sizeof(char)*32);  
            memset(readuser.password,'\0',sizeof(char)*32); 
            while (i!=readuser.last)
            {
                readuser.data[i]=readuser.data[readuser.last+1];
                i++;
            }
            readuser.last=0;
            fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
            fwrite(&readuser,sizeof(seqlist_t),1,fp);
            break;     
        }
        else
        {
            printf("输入的密码错误!\n");
        }
    }
    
    printf("用户已注销!\n");
    printf("**********************");
    printf("\n");
    fclose(fp);
}
